package com.xros.demo.gamedataserver.handler;

import com.xros.demo.gamedataserver.utils.ArrayUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandler;

/**
 * @Author: mzp
 * @Description: 前置输入的handler
 * @Date: 2024/4/8 10:17
 * @Version: 1.0
 */
public class PrefixInboundHandler implements ChannelInboundHandler {
    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {

    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {

    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {

    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {

    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("收到原始消息：" + msg);
        if (msg instanceof ByteBuf) {

            ByteBuf org = ((ByteBuf) msg);

            byte[] bytes = new byte[org.readableBytes()];

            org.getBytes(0, bytes);

            final String bytesArrStr = ArrayUtils.getBytesArrStr(bytes);

            System.out.println("收到原始消息 的字节数组形式：" + bytesArrStr);

        }
        ctx.fireChannelRead(msg);

    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {

    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

    }

    @Override
    public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {

    }

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {

    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {

    }
}
